/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import model.GroupAccount;

/**
 *
 * @author adriano.lourenco
 */

public class GroupAccountDAO implements DAO {

    private final DataSource dataSource;

    public GroupAccountDAO() {
        dataSource = new DataSource();
    }

    
    
    public void create(Object o) {
        
        try {

            String sql = "INSERT INTO groupaccount VALUES (null,?,?,?)";
            PreparedStatement ps = dataSource.getConnection().prepareStatement(sql);
            GroupAccount groupAccount = (GroupAccount) o;
            
     
            ps.setString(1,groupAccount.getLstStatus().toUpperCase());
            ps.setString(2, groupAccount.getLstTipo().toUpperCase());
            ps.setString(3, groupAccount.getLstGrupoDeContas().toUpperCase());
          

            int res = ps.executeUpdate();
            ps.close();
            if (res == 0) {
                System.err.println("GROUPACCOUNTDAO.CREATE - Nenhum  Grupo de Conta criado !");

            }
        } catch (SQLException e) {
            System.err.println("GROUPACCOUNTDAO.CREATE  - " + e.getMessage());

        }
    }
    
    public Object findAll() {
        try {
            String sql = "SELECT * FROM groupaccount";

            PreparedStatement ps = dataSource.getConnection().prepareStatement(sql);
            ArrayList<GroupAccount> listaGrupoContas = new ArrayList<GroupAccount>();
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                  
                GroupAccount ga= new GroupAccount();
                
                ga.setIdgroupaccount(rs.getString("GroupAccount.idgroupaccount"));
                ga.setLstGrupoDeContas(rs.getString("GroupAccount.descricao"));
                
                listaGrupoContas.add(ga);
            
            }
            return listaGrupoContas;

        } catch (Exception e) {
            System.err.println("GROUPACCOUNT.READ - " + e.getMessage());
            return null;
        }
        
        
        
    }

     public Object find(String key) {

        try {

            String sql = "SELECT * FROM groupaccount WHERE descricao = ?";
            PreparedStatement ps = dataSource.getConnection().prepareStatement(sql);

            ps.setString(1, key);

            ResultSet rs = ps.executeQuery();
          
            if (rs.next()) {
                GroupAccount ga = new GroupAccount();
                
                ga.setIdgroupaccount(rs.getString("id"));
                ga.setLstGrupoDeContas(rs.getString("descricao"));
               
                return ga;
            }
        } catch (SQLException e) {
            System.err.println("GROUPACCOUNTDAO.READ - " + e.getMessage());
        }
        return null;

    }
    
    
    
    
    public void delete(Object o) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    public void update(Object o) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    public Object read(Object o) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    public void closeDAO() {
        dataSource.closeConnection();
    }
    
    
    

}
